package org.bravof.differ.util;

import lombok.extern.log4j.Log4j2;
import org.bravof.differ.entity.dbbase.TableField;
import org.bravof.differ.entity.enums.FiledTypeEnum;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

@Log4j2
public class FieldUtil {
    /**
     * 将从mysql中查询获取到的字段描述文本转换为 TableField 实例中的变量
     *
     * @param field
     * @param typeFromSql
     */
    public static void handleTableField4Type(TableField field, String typeFromSql) {
        log.info("处理表字段sql -> " + typeFromSql);
        // 转换为小写
        Assert.notNull(typeFromSql, "获取数据库字段描述失败");

        // FIXME 此处的替换不应当，临时处理一下
        typeFromSql = typeFromSql.replace("unsigned", "").trim();

        typeFromSql = typeFromSql.toLowerCase(Locale.ROOT);

        String typeMetaStr;// 字段类型，mysql类型
        String lengthMetaStr = null;// 字段类型，mysql类型
        String secondLengthMetaStr = null;// 字段类型，mysql类型
        List<String> enumValueList = null;// 枚举值列表


        // 是否存在括号？
        if (typeFromSql.contains(CommonConst.SYMBOL_BRACKETS_LEFT)) {
            int position = typeFromSql.indexOf(CommonConst.SYMBOL_BRACKETS_LEFT);
            typeMetaStr = typeFromSql.substring(0, position);
            lengthMetaStr = typeFromSql.substring(position + 1);
            lengthMetaStr = lengthMetaStr.substring(0, lengthMetaStr.length() - 1);

            if (lengthMetaStr.contains("enum")) {
                String text = lengthMetaStr.replace("enum", "").replace("(", "").replace(")", "");
                String[] split = text.split(CommonConst.SYMBOL_COMMA);
                enumValueList = new ArrayList<>();
                for (String s : split) {
                    enumValueList.add(s.replace("'", ""));
                }
            } else {
                // 如果存在逗号，表示数据有精度
                String[] array = lengthMetaStr.split(CommonConst.SYMBOL_COMMA);
                lengthMetaStr = array[0];
                lengthMetaStr = lengthMetaStr.replace(CommonConst.SYMBOL_BRACKETS_RIGHT, "");
                if (array.length == 2) {
                    secondLengthMetaStr = array[1];
                    secondLengthMetaStr = secondLengthMetaStr.replace(CommonConst.SYMBOL_BRACKETS_RIGHT, "");
                }
            }
        } else {
            typeMetaStr = typeFromSql;
        }

        log.info("lengthMetaStr=" + lengthMetaStr);
        log.info("secondLengthMetaStr=" + secondLengthMetaStr);

        field.setFieldTypeEnum(FiledTypeEnum.valueOf(typeMetaStr.toUpperCase(Locale.ROOT)));
        if (null != enumValueList) {
            field.setEnumValueList(enumValueList);
        } else {
            try {
                if (!typeMetaStr.startsWith(FiledTypeEnum.ENUM.name().toLowerCase(Locale.ROOT))) {
                    field.setLength(null != lengthMetaStr ? Integer.parseInt(lengthMetaStr) : 0);
                    field.setSecondLength(null != secondLengthMetaStr ? Integer.parseInt(secondLengthMetaStr) : 0);
                }
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 赋值字段
     *
     * @param field
     * @param type
     */
    public static void handleTableField4Null(TableField field, String type) {
        switch (type) {
            case "YES":
                field.setNotNull(Boolean.TRUE);
                break;
            case "NO":
                field.setNotNull(Boolean.FALSE);
                break;
        }
    }

    /**
     * 赋值字段
     *
     * @param field
     * @param type
     */
    public static void handleTableField4AutoIncr(TableField field, String type) {
        if (type.contains("auto_increment")) {
            field.setAutoIncr(Boolean.TRUE);
        } else {
            field.setAutoIncr(Boolean.FALSE);
        }
    }
}
